package com.tencent.sr.iris.activity.service.event.task;

import com.alibaba.fastjson.JSON;
import com.tencent.sr.iris.activity.common.enums.AwardRecordIssueStatusEnum;
import com.tencent.sr.iris.activity.common.enums.UserTaskStatusEnum;
import com.tencent.sr.iris.activity.common.util.TimeUtils;
import com.tencent.sr.iris.activity.domain.event.dto.ActivityDTO;
import com.tencent.sr.iris.activity.domain.event.dto.ActivityTaskDTO;
import com.tencent.sr.iris.activity.interfaces.event.dto.ActivityEventDTO;
import com.tencent.sr.iris.activity.repository.entity.TIrisActivityAwardRecordDO;
import com.tencent.sr.iris.activity.repository.entity.TIrisUserTaskRecordDO;
import com.tencent.sr.iris.activity.service.activity.ActivityCommonService;
import com.tencent.sr.iris.activity.service.activity.IrisActivityAwardRecordService;
import com.tencent.sr.iris.activity.service.activity.IrisUserTaskRecordService;
import com.tencent.sr.iris.activity.service.event.vo.BaseUserTaskDataVO;
import com.tencent.sr.iris.activity.service.event.vo.TaskExecutorExtendInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Objects;

import static com.tencent.sr.iris.activity.common.util.TimeUtils.getCurrentDayLastSecondLocalDateTime;

/**
 * Copyright (C) 2023 Limited, a Tencent company. All rights reserved.
 * 辅导津贴任务执行器
 *
 * @author p_htaozhou@tencent.com
 * @create 2023/8/28 16:09
 */

@Slf4j
@Component
public class GuideAllowanceTaskExecutor extends AbstractIrisTaskExecutor {

    @Resource
    private
    IrisActivityAwardRecordService irisActivityAwardRecordService;

    @Resource
    private ActivityCommonService activityCommonService;

    @Resource
    private IrisUserTaskRecordService userTaskRecordService;

    @Override
    public void process(ActivityEventDTO msg, ActivityDTO activity, ActivityTaskDTO task,
                        List<String> eventHandingInfoList, TaskExecutorExtendInfo taskExecutorExtendInfo) {
        log.info("【辅导津贴任务执行器】开始执行");
        // 1、校验
        String period = LocalDate.now().toString();
        TIrisUserTaskRecordDO userTaskRecordDO = queryUserTaskRecord(msg.getUid(),
                activity.getBizId(), task.getBizId(), period);
        log.info("【辅导津贴任务执行器】辅导津贴任务完成进度:{}", JSON.toJSONString(userTaskRecordDO));

        if (Objects.nonNull(userTaskRecordDO)) {
            if (UserTaskStatusEnum.FINISHED.getCode() == userTaskRecordDO.getUserTaskStatus()) {
                eventHandingInfoList.add("辅导津贴任务已完成，不重新执行");
                log.info("【辅导津贴任务执行器】辅导津贴，下级用户已完成任务,uid:{},ActivityEventDTO:{},", msg.getUid(), JSON.toJSONString(msg));
                return;
            }
        } else {
            // 2、插入任务至任务记录表
            userTaskRecordDO = buildUserTaskRecordDO(activity, task, msg);
            eventHandingInfoList.add("辅导津贴任务达成");
            log.info("【辅导津贴任务执行器】辅导津贴任务达成,uid:{},ActivityEventDTO:{}", msg.getUid(), JSON.toJSONString(msg));
        }

        // 3、发奖
        TIrisActivityAwardRecordDO awardRecordDO = executeAward(task, eventHandingInfoList, userTaskRecordDO);

        // 4、记录任务进度
        userTaskRecordService.updateUserTaskRecordStatus(userTaskRecordDO, awardRecordDO);
        log.info("【辅导津贴任务执行器】执行结束");
    }

    /**
     * 执行发奖
     *
     * @param task                 任务
     * @param eventHandingInfoList eventHandingInfoList
     * @param userTaskRecordDO     userTaskRecordDO
     */
    private TIrisActivityAwardRecordDO executeAward(ActivityTaskDTO task, List<String> eventHandingInfoList,
                              TIrisUserTaskRecordDO userTaskRecordDO) {
        String uid = userTaskRecordDO.getUid();
        String taskId = userTaskRecordDO.getTaskId();
        String taskPeriod = userTaskRecordDO.getTaskPeriod();

        Date startDate = TimeUtils.getCurrentDayFirstSecondTime();
        Date endDate = TimeUtils.getCurrentDayLastSecondTime();
        TIrisActivityAwardRecordDO awardRecordDO = null;
        List<TIrisActivityAwardRecordDO> list = irisActivityAwardRecordService.queryIssueList(
                userTaskRecordDO.getActivityId(), taskId, uid, startDate, endDate);
        if (CollectionUtils.isNotEmpty(list)) {
            eventHandingInfoList.add("辅导津贴已发过奖，不在调用发奖接口，直接返回成功");
        } else {
            //发放权益
            awardRecordDO = activityCommonService.sendAwardForSupervisor(userTaskRecordDO, task);
            if (awardRecordDO != null
                    && AwardRecordIssueStatusEnum.ISSUE_SUCCEED.getCode() == awardRecordDO.getIssueStatus()) {
                eventHandingInfoList.add("辅导津贴任务发奖成功");
                // 设置发奖标识
                irisActivityAwardRecordService
                        .setAwardTimeToday(userTaskRecordDO.getTaskId(), uid, taskPeriod, getCurrentDayLastSecondLocalDateTime());
            } else {
                eventHandingInfoList.add("辅导津贴任务发奖失败");
            }
        }
        return awardRecordDO;
    }

    @Override
    public void fillTaskRecordStatus(ActivityEventDTO eventDTO, ActivityDTO act, ActivityTaskDTO task,
                                     TIrisUserTaskRecordDO userTaskRecordDO, LocalDate currentLocalDate) {

    }

    @Override
    public boolean executeAward(ActivityEventDTO event, ActivityDTO act, ActivityTaskDTO task) {
        return false;
    }

    @Override
    public boolean checkTaskThreshold(ActivityEventDTO event, ActivityDTO act, ActivityTaskDTO task,
                                      BaseUserTaskDataVO userTaskDataVO) {
        return false;
    }

    @Override
    public BaseUserTaskDataVO prepareUserTaskData(ActivityEventDTO event, ActivityDTO act, ActivityTaskDTO task) {
        return null;
    }
}
